<?php

include_once 'Room.php';

class RoomGroup {

    /**
     * @access public
     * @var Room[]
     */
    public $Room;

    public function RoomGroup($rooms = NULL, $count = 0) {
        for ($index = 1; $index <= $count; $index++) {
            if (gettype($rooms[$index]) !== "undefined" &&
                    gettype($rooms[$index]) !== "null") {
                if (gettype($rooms[$index]) !== "object") {
                    $this->Room[$index] = new Room();
                    $this->Room[$index]->numberOfAdults =
                            $rooms[$index]["adultsCount"];
                    $this->Room[$index]->numberOfChildren =
                            $rooms[$index]["childrenCount"];
                    for ($childIndex = 0; $childIndex < $this->Room[$index]->numberOfChildren; $childIndex++) {
                        $this->Room[$index]->childAges[$childIndex] =
                                $rooms[$index]["children" . ($childIndex + 1)];
                    }
                } else {
                    break;
                }
            }
        }
    }

    public function getRoomGroupRest() {
        $result = array();
        for ($i = 0; $i < count($this->Room); $i++) {
            $result ["room" . ($i + 1)] = $this->Room[$i + 1]->numberOfAdults;
            $vars = $this->Room[$i + 1];
            foreach ($vars as $key => $value) {
                if (($key != "numberOfChildren" || $key != "numberOfAdults" || $key != "childAges") && $value != NULL && $value != "")
                    $result["room" . ($i + 1).ucfirst($key)] = $value;
            }
            for ($j = 0; $j < count($this->Room[$i + 1]->childAges); $j++) {
                $result ["room" . ($i + 1)] .= "," . $this->Room[$i + 1]->childAges[$j];
            }
        }
        return $result;
    }

    public function GetRestResponse() {
        $args = $this->getRoomGroupRest();
        $result = "";
        foreach ($args as $key => $value) {
            if ($value != NULL || $value != "")
                $result .= "&" . $key . "=" . $value;
        }
        return $result;
    }

}